unit AfterDownload_TestRuleForm;

interface

uses
  AfterDownload_Defines,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, JvExStdCtrls, JvListBox, Buttons, ComCtrls;

type
  TAD_TestRuleForm = class(TForm)
    JLB_Log: TJvListBox;
    Label1: TLabel;
    PB_Log: TProgressBar;
    SB_TestRule: TSpeedButton;
    BitBtn1: TBitBtn;
    CB_NotMatched: TCheckBox;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure SB_TestRuleClick(Sender: TObject);
  private
    { Private declarations }
    Lang: String;
    FRule: Rule;
    procedure TranslateToo;
    procedure SetLanguage(language: String);
    procedure SetRule(const NewRule: Rule);
  public
    { Public declarations }
    property Language: String write SetLanguage;
    property myRule: Rule read FRule write SetRule;
  end;

var
  AD_TRF: TAD_TestRuleForm = nil;

implementation

{$R *.dfm}

uses
  gnugettext,
  Locales,
  Tokens;

procedure TAD_TestRuleForm.FormCreate(Sender: TObject);
begin
 TranslateComponent(Self);
 FRule:=Rule.Create(nil);
end;

procedure TAD_TestRuleForm.FormDestroy(Sender: TObject);
begin
 FreeAndNil(FRule);
end;

procedure TAD_TestRuleForm.SetLanguage(language: String);
begin
 Lang:=language;
 TranslateForm(Self, Language, TranslateToo);
end;

procedure TAD_TestRuleForm.TranslateToo;
begin
 BitBtn1.Caption:=_('Close');
end;

procedure TAD_TestRuleForm.SetRule(const NewRule: Rule);
begin
 FRule.Assign(NewRule);
end;

procedure TAD_TestRuleForm.SB_TestRuleClick(Sender: TObject);
 var
   i, t: Integer;
   Info, fn: string;
begin
 JLB_Log.Items.Clear;
 PB_Log.Position:=0;
 PB_Log.Max:=myInfo.Count;
 //
 for i:=0 to myInfo.Count-1 do
  begin
   Info:=myInfo.ValueFromIndex[i];
   fn:=ExtractToken('saveto', Info);
   if CB_NotMatched.Checked then
     JLB_Log.Items.Add(Format(_('Testing file %s ...'), [fn]));
   //
   if myRule.Matched(Info) then
    begin
     JLB_Log.Items.Add(Format(_('File %s is matched by rule %s.'), [fn, myRule.Title]));
     //
     for t:=0 to myRule.Actions.Count-1 do
      begin
       if myRule.Actions[i].Enabled then
         JLB_Log.Items.Add(Format(_('Action "%s %s" is performing with file %s.'), [GetActTypeDesc(myRule.Actions[t].ActType), myRule.Actions[t].Destination, fn]));
      end;
    end;
   PB_Log.Position:=i+1;
   Application.ProcessMessages;
  end;
end;

end.
